package cn.rokhdelar.umbrsapi.core.controller;


import cn.rokhdelar.umbrsapi.core.entity.AbstractEntity;
import cn.rokhdelar.umbrsapi.core.entity.BaseDto;
import cn.rokhdelar.umbrsapi.core.service.BaseService;
import cn.rokhdelar.umbrsapi.core.utils.Result;
import cn.rokhdelar.umbrsapi.core.utils.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * BaseController
 * <p>Title: BaseController</p>  
 * <p>Description: </p>  
 * @author liucy  
 * @date 2023年12月19日
 */
@Controller
public abstract class BaseController<T extends AbstractEntity>  extends Result {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5084573433138927673L;

	protected static final Log log = LogFactory.getLog(BaseController.class);

	/**
	 * Controller泛型对象默认的Service
	 */
	private BaseService<T> baseService;

	/**
	 * 实现此方法,返回对应的实体对象
	 * 
	 * @return
	 */
	protected abstract Class<T> getEntityClass();

	protected String prefix;


	protected T entity;

	/**
	 * 默认返回BASE_SERVICE OBJECT
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public BaseService<T> getBaseService() {
		String serviceName = StringUtils.uncapitalize(this.getEntityClass().getSimpleName()) + "Service";
		if (baseService == null) {
			log.debug("find baseService=" + serviceName);
			try {
				baseService = (BaseService<T>) SpringUtil.getBean(serviceName);
			} catch (BeansException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (baseService == null) {
			log.error("Not found bean defined for '" + serviceName + "'");
		}
		return baseService;
	}

	/**
	 * 构造方法
	 */
	public BaseController() {
		//super();
		try {
			this.entity = this.getEntityClass().newInstance();
		} catch (InstantiationException e) {
			log.error(e.getStackTrace());
		} catch (IllegalAccessException e) {
			log.error(e.getStackTrace());
		}
	}

	@RequestMapping(value = "/auth/logicDeleteById", method = RequestMethod.POST)
	@ResponseBody
	public Result logicDeleteById(@RequestBody BaseDto dto) throws Exception
	{
		Long id=dto.getId();
		beforeLogicDelete(id);
		getBaseService().doLogicDeleteByPrimaryKey(id);
		afterLogicDelete(id);
		return super.success();
	}



	@RequestMapping(value = "/auth/deleteById", method = RequestMethod.POST)
	@ResponseBody
	public Result deleteById(Long id) throws Exception
	{
		getBaseService().doDeleteByPrimaryKey(id);
		return super.success();
	}

	@RequestMapping(value = "/auth/deleteByIds", method = RequestMethod.POST)
	@ResponseBody
	public Result deleteByIds(String ids) throws Exception
	{
		List<Long> sidList=  Arrays.stream(ids.split(",")) // 使用逗号分割字符串
				.map(String::trim) // 去除可能的空白字符
				.mapToLong(Long::parseLong) // 将字符串转换为long
				.boxed() // 将long转换为Long，以便放入List<Long>
				.collect(Collectors.toList()); // 收集到List中  ;
		for(Long sid:sidList){
			getBaseService().doDeleteByPrimaryKey(sid);
		}
		return super.success();
	}


	@RequestMapping(value = "/auth/logicDeleteByIds", method = RequestMethod.POST)
	@ResponseBody
	public Result logicDeleteByIds(@RequestBody BaseDto dto) throws Exception
	{
		List<Long> sidList= dto.getIds();
		beforeLogicAllDelete(dto.getIds());
		for(Long sid:sidList){
			getBaseService().doLogicDeleteByPrimaryKey(sid);
		}
		afterLogicAllDelete(dto.getIds());
		return super.success();
	}

	@RequestMapping(value = "/auth/findById", method = RequestMethod.POST)

	@ResponseBody
	public Result findById(Long id) throws Exception
	{
		T entity=getBaseService().findByPrimaryKey(id);
//		return super.success(entity,null);
		return super.success();
	}

	@RequestMapping(value = "/disabledById", method = RequestMethod.POST)
	@ResponseBody
	public Result disabledById(@RequestBody BaseDto dto) throws Exception
	{
		beforeDisabled(dto.getId());
		getBaseService().doDisabledById(dto.getId(),dto.getStatus());
		afterLogicDelete(dto.getId());
		return super.success();
	}

	protected void beforeDisabled(Long id) throws Exception{}


	/**
	 * 在调用逻辑删除前的操作
	 * @param id
	 * @throws Exception
	 */
	protected void beforeLogicDelete(Long id) throws Exception{}
	
	/**
	 * 批量逻辑删除前
	 * <p>Title: beforeLogicAllDelete</p>  
	 * <p>Description: </p>  
	 * @author liucy   
	 * @date 2023年12月22日  
	 * @param Ids
	 * @throws Exception
	 */
	protected void beforeLogicAllDelete(List<Long> Ids) throws Exception{}

	/**
	 * 在调用逻辑删除后的操作
	 * @param id
	 * @throws Exception
	 */
	protected void afterLogicDelete(Long id) throws Exception{}
	
	/**
	 * 批量逻辑删除后
	 * <p>Title: afterLogicAllDelete</p>  
	 * <p>Description: </p>  
	 * @author liucy   
	 * @date 2023年12月22日  
	 * @param id
	 * @throws Exception
	 */
	protected void afterLogicAllDelete(List<Long> Ids) throws Exception{}

	/**
	 * 在调用物理删除前的操作
	 * @param id
	 * @throws Exception
	 */
	protected void beforeDelete(Long id) throws Exception{}

	/**
	 * 在调用物理删除后的操作
	 * @param id
	 * @throws Exception
	 */
	protected void afterDelete(Long id) throws Exception{}

}
